package br.inf.solutions.apresentacoes.ssp.view.composer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.zkoss.zk.ui.Component;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Textbox;

import br.com.vexillum.util.ReflectionUtils;
import br.com.vexillum.util.Return;
import br.com.vexillum.util.SpringFactory;
import br.inf.solutions.apresentacoes.ssp.control.CaliberController;
import br.inf.solutions.apresentacoes.ssp.control.CountryOfManufactureController;
import br.inf.solutions.apresentacoes.ssp.control.FinishingController;
import br.inf.solutions.apresentacoes.ssp.control.FunctioningController;
import br.inf.solutions.apresentacoes.ssp.control.OwnerWeaponController;
import br.inf.solutions.apresentacoes.ssp.control.PreservationController;
import br.inf.solutions.apresentacoes.ssp.control.ProviderController;
import br.inf.solutions.apresentacoes.ssp.control.ResourceController;
import br.inf.solutions.apresentacoes.ssp.control.SpeciesController;
import br.inf.solutions.apresentacoes.ssp.control.TypeWeaponController;
import br.inf.solutions.apresentacoes.ssp.control.WeaponController;
import br.inf.solutions.apresentacoes.ssp.control.basics.MarkController;
import br.inf.solutions.apresentacoes.ssp.control.basics.ModelWeaponController;
import br.inf.solutions.apresentacoes.ssp.model.Caliber;
import br.inf.solutions.apresentacoes.ssp.model.CountryOfManufacture;
import br.inf.solutions.apresentacoes.ssp.model.Finishing;
import br.inf.solutions.apresentacoes.ssp.model.Functioning;
import br.inf.solutions.apresentacoes.ssp.model.OwnerWeapon;
import br.inf.solutions.apresentacoes.ssp.model.Preservation;
import br.inf.solutions.apresentacoes.ssp.model.Provider;
import br.inf.solutions.apresentacoes.ssp.model.Resource;
import br.inf.solutions.apresentacoes.ssp.model.Species;
import br.inf.solutions.apresentacoes.ssp.model.TypeWeapon;
import br.inf.solutions.apresentacoes.ssp.model.Weapon;
import br.inf.solutions.apresentacoes.ssp.model.basics.Mark;
import br.inf.solutions.apresentacoes.ssp.model.basics.Model;
import br.inf.solutions.apresentacoes.ssp.model.enums.DirectionRays;

@SuppressWarnings("serial")
@Service
@Scope("prototype")
public class WeaponComposer extends BaseComposer<Weapon, WeaponController> {

	protected Mark searchMark;
	
	public Mark getSearchMark() {
		return searchMark;
	}

	public void setSearchMark(Mark searchMark) {
		this.searchMark = searchMark;
	}

	@Override
	protected String getUpdatePage() {
		return null;
	}

	@Override
	protected String getDeactivationMessage() {
		return null;
	}
	
	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		loadBinder();
	}

	@Override
	public WeaponController getControl() {
		return SpringFactory.getController("weaponController",
				WeaponController.class,
				ReflectionUtils.prepareDataForPersistence(this));
	}

	@Override
	public Weapon getEntityObject() {
		return new Weapon();
	}

	@SuppressWarnings("unchecked")
	public List<Mark> getMarks() {
		MarkController controlador = SpringFactory.getController(
				"markController", MarkController.class,
				ReflectionUtils.prepareDataForPersistence(this));
		return (List<Mark>) controlador.doAction("listAll").getList();
	}

	@SuppressWarnings("unchecked")
	public List<Model> getListModel() {
		if (searchMark == null){
			return new ArrayList<Model>();
		}
		else{
			HashMap<String, Object> data = new HashMap<String, Object>();
		
			Model model = new Model();
			model.setMark(searchMark);
			
			data.put("entity", model);
			ModelWeaponController controlador = SpringFactory.getController("modelWeaponController", ModelWeaponController.class, data);
			return (List<Model>) controlador.doAction("searchByHQL").getList();
		}
	}

	@SuppressWarnings("unchecked")
	public List<Species> getSpecies() {
		SpeciesController controlador = SpringFactory.getController(
				"speciesController", SpeciesController.class,
				ReflectionUtils.prepareDataForPersistence(this));
		return (List<Species>) controlador.doAction("listAll").getList();
	}

	@SuppressWarnings("unchecked")
	public List<Caliber> getCalibers() {
		CaliberController controlador = SpringFactory.getController(
				"caliberController", CaliberController.class,
				ReflectionUtils.prepareDataForPersistence(this));
		return (List<Caliber>) controlador.doAction("listAll").getList();
	}

	@SuppressWarnings("unchecked")
	public List<CountryOfManufacture> getCountryOfManufacturies() {
		CountryOfManufactureController controlador = SpringFactory
				.getController("countryOfManufactureController",
						CountryOfManufactureController.class,
						ReflectionUtils.prepareDataForPersistence(this));
		return (List<CountryOfManufacture>) controlador.doAction("listAll")
				.getList();
	}

	@SuppressWarnings("unchecked")
	public List<TypeWeapon> getTypesWeapon() {
		TypeWeaponController controlador = SpringFactory.getController(
				"typeWeaponController", TypeWeaponController.class,
				ReflectionUtils.prepareDataForPersistence(this));
		return (List<TypeWeapon>) controlador.doAction("listAll").getList();
	}

	@SuppressWarnings("unchecked")
	public List<Functioning> getFunctionings() {
		FunctioningController controlador = SpringFactory.getController(
				"functioningController", FunctioningController.class,
				ReflectionUtils.prepareDataForPersistence(this));
		return (List<Functioning>) controlador.doAction("listAll").getList();
	}

	@SuppressWarnings("unchecked")
	public List<Finishing> getFinishings() {
		FinishingController controlador = SpringFactory.getController(
				"finishingController", FinishingController.class,
				ReflectionUtils.prepareDataForPersistence(this));
		return (List<Finishing>) controlador.doAction("listAll").getList();
	}

	@SuppressWarnings("unchecked")
	public List<Resource> getResources() {
		ResourceController controlador = SpringFactory.getController(
				"resourceController", ResourceController.class,
				ReflectionUtils.prepareDataForPersistence(this));
		return (List<Resource>) controlador.doAction("listAll").getList();
	}

	@SuppressWarnings("unchecked")
	public List<Preservation> getPreservations() {
		PreservationController controlador = SpringFactory.getController(
				"preservationController", PreservationController.class,
				ReflectionUtils.prepareDataForPersistence(this));
		return (List<Preservation>) controlador.doAction("listAll").getList();
	}

	@SuppressWarnings("unchecked")
	public List<Provider> getProviders() {
		ProviderController controlador = SpringFactory.getController(
				"providerController", ProviderController.class,
				ReflectionUtils.prepareDataForPersistence(this));
		return (List<Provider>) controlador.doAction("listAll").getList();
	}
	
	public List<DirectionRays> getDirectionsRays() {
		return Arrays.asList(DirectionRays.values());
	}
	
	@SuppressWarnings("unchecked")
	public List<OwnerWeapon> getOwnersWeapon() {
		OwnerWeaponController controlador = SpringFactory.getController(
				"ownerWeaponController", OwnerWeaponController.class,
				ReflectionUtils.prepareDataForPersistence(this));
		return (List<OwnerWeapon>) controlador.doAction("listAll").getList();
	}

	
	public Return searchWeapon(){
		Return ret = getControl().doAction("searchWeapon");				
		
		if(ret.isValid()){
			if(ret.getList() != null && !ret.getList().isEmpty())			
				setEntity((Weapon) ((Weapon) (ret.getList().get(0))).cloneEntity());			
			else
				clear();
		}			
		loadBinder();
		loadComboboxes();
		return ret;
	}

	
	public void loadComboboxes() {
		Textbox tb;
		Combobox cb;

		tb = (Textbox) getComponentById(component, "fldMarkHidden");
		cb = (Combobox) getComponentById(component, "fldMark");

		if (tb != null) {
			cb.setValue(tb.getValue());
			setSearchMark(new Mark(tb.getValue()));
		}

		tb = (Textbox) getComponentById(component, "fldModelHidden");
		cb = (Combobox) getComponentById(component, "fldModel");

		if (tb != null) {
			cb.setValue(tb.getValue());
		}
	}
	
	public void clear() {
		Weapon weapon = new Weapon();
		weapon.setNumberWeapon(entity.getNumberWeapon());	
		super.clearForm();
		entity = weapon;
	}
	
	
	
	@Override
	public Return saveEntity() {
		Return ret = new Return(true);		
		ret.concat(super.saveEntity());
		
		if(ret.isValid()){
			((Combobox)getComponentById(component, "fldMark")).setSelectedItem(null);
			((Combobox)getComponentById(component, "fldTypeWeapon")).setSelectedItem(null);
		}
		treatReturn(ret);
		return ret;
	}
}